import {
  addCategory,
  addCategoryCustom,
  deleteCategory,
  getAllCategory,
  getCategory,
  getCategoryTree,
  updateCategory,
  getCategoryEffect,
  getCategoryRepulse,
} from '@/services/goods/groupManageApproval';

const group = {
  namespace: 'groupManageApproval',
  state: {
    categoryList: [],
    categoryTree: [],
    page: 1,
    pageSize: 20,
    searchParams: {},
    total: 0,
    tableData: [],
    platformList: [],
    addOpen: false,
    drawerType: '',
    drawerParams: {},
    message: '',
  },
  reducers: {
    updateState: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
  effects: {
    *getAllCategory(_, { call, put }) {
      const { code, data } = yield call(getAllCategory);
      if (code === 200 && data) {
        yield put({
          type: 'updateState',
          payload: {
            categoryList: data || [],
          },
        });
      }
    },
    *getCategoryTree(_, { call, put }) {
      const { code, data } = yield call(getCategoryTree);
      if (code === 200 && data) {
        yield put({
          type: 'updateState',
          payload: {
            categoryTree: data || [],
          },
        });
      }
    },

    *selectPage({ payload }, { call, put, select }) {
      const { groupManageApproval } = yield select(({ groupManageApproval }) => ({
        groupManageApproval,
      }));
      const obj = {
        page: payload?.page ? payload?.page : groupManageApproval.page,
        pageSize: payload?.pageSize ? payload?.pageSize : groupManageApproval.pageSize,
      };

      if (groupManageApproval?.searchParams?.categoryName?.label) {
        obj.categoryName = groupManageApproval?.searchParams?.categoryName?.label;
      }

      const { code, data, msg, total } = yield call(getCategory, obj);
      let tableData = [];
      if (code === 200 && data) {
        //
        tableData = data || [];
      }
      yield put({
        type: 'updateState',
        payload: {
          tableData,
          msg,
          total: total || 0,
        },
      });
    },

    // 新增(平台)
    *addCategory({ payload }, { call, put }) {
      const { code } = yield call(addCategory, payload.searchParams);
      if (code === 200) {
        yield put({
          type: 'updateState',
          payload: {
            addOpen: false,
            drawerParams: {},
          },
        });
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },

    // 新增(客户)
    *addCategoryCustom({ payload }, { call, put }) {
      const { code } = yield call(addCategoryCustom, payload.searchParams);
      if (code === 200) {
        yield put({
          type: 'updateState',
          payload: {
            addOpen: false,
            drawerParams: {},
          },
        });
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },

    *updateCategory({ payload }, { call, put, select }) {
      const groupManageApproval = yield select(({ groupManageApproval }) => groupManageApproval);
      const { drawerParams } = groupManageApproval;

      const { code } = yield call(updateCategory, {
        ...payload.searchParams,
        id: drawerParams?.id,
      });
      if (code === 200) {
        //
        yield put({
          type: 'updateState',
          payload: {
            addOpen: false,
            drawerParams: {},
          },
        });
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },

    *deleteCategory({ payload }, { call, put }) {
      const { code } = yield call(deleteCategory, payload);
      if (code === 200) {
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },

    // 同意
    *getCategoryEffect({ payload }, { call, put }) {
      const { code } = yield call(getCategoryEffect, payload);
      if (code === 200) {
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },

    // 拒绝
    *getCategoryRepulse({ payload }, { call, put }) {
      const { code } = yield call(getCategoryRepulse, payload);
      if (code === 200) {
        yield put({
          type: 'updateState',
          payload: {
            addOpen: false,
            drawerParams: {},
          },
        });
        yield put({
          type: 'getCategoryTree',
        });
        yield put({
          type: 'getAllCategory',
        });
        payload.actionRef.current?.reload();
      }
    },
  },
};

export default group;
